package com.zhongketech.sunflower.transport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;

import com.google.gson.Gson;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.transport.model.Applications;
import com.zhongketech.sunflower.transport.model.Authorizations;
import com.zhongketech.sunflower.transport.model.AuthorizationsDao;
import com.zhongketech.sunflower.transport.model.MyApplicationsInfo;
import com.zhongketech.sunflower.transport.model.MyContacts;
import com.zhongketech.sunflower.transport.model.MyRequest;
import com.zhongketech.sunflower.transport.model.MySupervision;
import com.zhongketech.sunflower.transport.model.Parents;
import com.zhongketech.sunflower.transport.model.Request;
import com.zhongketech.sunflower.transport.model.SmsCode;
import com.zhongketech.sunflower.transport.model.SpecialNums;
import com.zhongketech.sunflower.transport.model.Supervision;
import com.zhongketech.sunflower.transport.model.TestMySupervision;
import com.zhongketech.sunflower.transport.model.User;
import com.zhongketech.sunflower.transport.uitl.DataUtils;
import com.zhongketech.sunflower.transport.uitl.NetTransportUtil;

public class Action {

	private  class AuthorizationsUserJson
	{
		private Authorizations user;

		@SuppressWarnings("unused")
		public AuthorizationsUserJson(final Authorizations user)
		{
			setUser(user);
		}

		public Authorizations getUser() {
			return user;
		}

		public void setUser(Authorizations user) {
			this.user = user;
		}
	}

	private  class userJson
	{
		private User user;

		@SuppressWarnings("unused")
		public userJson(final User user)
		{
			setUser(user);
		}

		public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}
	}

	private  class Supervisions
	{
		private List<Supervision> supervisons;

		@SuppressWarnings("unused")
		public Supervisions(final List<Supervision> supervison)
		{
			setSupervison(supervison);
		}


		public List<Supervision> getSupervison() {
			return supervisons;
		}

		public void setSupervison(List<Supervision> supervison) {
			this.supervisons = supervison;
		}
	}

	public boolean requestsState(String id,String state)
	{
		Map<String,String> userMap = new HashMap<String, String>();	
		userMap.put("state", state);
		String result = NetTransportUtil.getContentByPut("users/me/requests/"+id+"/state", userMap);
		LogUtil.i("Action","requestsState:"+result);
		if(null != result)
		{
			return true;
		}

		return false;
	}

	public Authorizations authorizations(String phone ,String password)
	{
		Map<String,String> userMap = new HashMap<String, String>();	
		userMap.put("password", password);
		userMap.put("device_id", DataUtils.getDeviceId());
		userMap.put("device_type", DataUtils.getDeviceType());
		userMap.put("device_version", DataUtils.getDeivcesVersion());
		userMap.put("push_token", null);
		userMap.put("phone", phone);
		userMap.put("push_token", "");
		userMap.put("app_version", DataUtils.getAppVersion());
		String result = NetTransportUtil.getContentByPost(InterfaceUrls.authorizations, userMap);
		LogUtil.i("Action","authorizations:"+result);
		Gson gson = new Gson();
		AuthorizationsUserJson userJson = gson.fromJson(result, AuthorizationsUserJson.class);
		if(null == userJson || null == userJson.getUser())
		{
			MyToast.getInstance().show("登录失败:Authorizations Can't get userJson ");
			return null;
		}

		//		AuthorizationsDao authorizationsDao = MyApplication.getDaoSession().getAuthorizationsDao();  
		//		authorizationsDao.insert(userJson.getUser());
		//		authorizationsDao.get


		return userJson.getUser();
	}

	public SmsCode smsCode(String phone)
	{
		SmsCode smsCode = null;
		Map<String,String> userMap = new HashMap<String, String>();	
		userMap.put("phone", MyApplication.getInstance().getPhone());
		String result = NetTransportUtil.getContent(InterfaceUrls.smsCode, userMap);
		LogUtil.i("Action","smsCode:"+result);
		Gson gson = new Gson();
		smsCode = gson.fromJson(result, SmsCode.class);
		return smsCode;
	}

	public User signUp(String phone,String password,String gender,String name,String birthday,String smsCode)
	{
		User user = null;
		Map<String,String> userMap = new HashMap<String, String>();	
		userMap.put("role", "student");
		userMap.put("phone", phone);
		userMap.put("password", password);
		userMap.put("gender", gender);
		userMap.put("name", name);
		userMap.put("birthday", birthday);
		userMap.put("device_id", DataUtils.getDeviceId());
		userMap.put("smscode", smsCode);

		String result = NetTransportUtil.getContentByPost(InterfaceUrls.signUp, userMap);
		LogUtil.i("Action","signUp:"+result);
		Gson gson = new Gson();
		user = gson.fromJson(result, User.class);

		return user;
	}

	public Parents myParents()
	{
		String result = NetTransportUtil.getContent("students/me/parents", null);
		LogUtil.i("Action","myParents:"+result);
		Gson gson = new Gson();
		return gson.fromJson(result, Parents.class);
	}

	public MyRequest requests()
	{
		String result = NetTransportUtil.getContent("users/me/requests", null);
		LogUtil.i("Action","requests:"+result);
		Gson gson = new Gson();

		try {
			return gson.fromJson(result, MyRequest.class);
		} catch (Exception e) {
			LogUtil.i("Action","e:"+e);
		}

		return null;
	}

	public MyContacts contacts()
	{
		String result = NetTransportUtil.getContent("/users/me/contacts", null);
		LogUtil.i("Action","contacts:"+result);
		Gson gson = new Gson();

		try {
			return gson.fromJson(result, MyContacts.class);
		} catch (Exception e) {
			LogUtil.i("Action","e:"+e);
		}

		return null;
	}

	public User user(String id)
	{
		User user = null;

		String result = NetTransportUtil.getContent("users/"+id, null);
		LogUtil.i("Action","user:"+result);
		Gson gson = new Gson();

		try {
			userJson json = gson.fromJson(result, userJson.class);
			if(null != json)
			{
				return json.getUser();
			}
		} catch (Exception e) {
			LogUtil.i("Action","e:"+e);
		}

		return user;
	}

	public boolean uploadApp(MyApplicationsInfo app) 
	{
		if(null == app)
		{
			LogUtil.i("Action","app is null");
			return false;
		}
		Map<String,String> userMap = new HashMap<String, String>();	
		userMap.put("id", app.getPackage_name());
		userMap.put("app_name", app.getApp_name());
		if(!(null == app.getImage_url()) && !("".equals(app.getImage_url())))
		{
			userMap.put("image_url", app.getImage_url());
		}

		String result = NetTransportUtil.getContentByPost("students/me/applications", userMap);
		return true;
	}

	public MySupervision supervisions() 
	{
		LogUtil.i("Action","get new supervisions");
		String result = NetTransportUtil.getContent("students/me/supervisions", null);
		Gson gson = new Gson();

		try {

			MySupervision svs = gson.fromJson(result, MySupervision.class);
			if(null != svs)
			{
				svs.setMySupervisionId("1");
				//				MyApplication.getInstance().getSupervisonsDaoSession().getMySupervisionDao().deleteAll();
				MyApplication.getInstance().getSupervisonsDaoSession().getMySupervisionDao().insertOrReplace(svs);
				//				MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().deleteAll();
				//				MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().loadDeep((long) 1);

				//String id = svs.getMySupervisionId();
				for(int i = 0; i < svs.getSupervisons().size();i ++)
				{
					MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().insertOrReplace(svs.getSupervisons().get(i));
					//MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().queryBuilder();
					//MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().loadDeep((long) 1);
					//					svs.getSupervisons().get(i).__setDaoSession(MyApplication.getInstance().getSupervisonsDaoSession());					

					LogUtil.i("Action","id["+i+"]:"+svs.getSupervisons().get(i).getId());
					LogUtil.i("Action","eyes["+i+"]:"+svs.getSupervisons().get(i).getEyes_protect());
					LogUtil.i("Action","app["+i+"]:"+svs.getSupervisons().get(i).getApplication());

					if(null != svs.getSupervisons().get(i).getApplication())
					{
						List<Applications> apps = svs.getSupervisons().get(i).getApplication().getApplications();
						for(int k = 0;k < apps.size();k ++)
						{
							LogUtil.i("Action","apps["+k+"] name:"+apps.get(k).getApp_name());
							LogUtil.i("Action","apps["+k+"] id:"+apps.get(k).getId());
							LogUtil.i("Action","apps["+k+"] image:"+apps.get(k).getImage_url());
							LogUtil.i("Action","apps["+k+"] enable:"+apps.get(k).getEnable());
						}
					}
					LogUtil.i("Action","call["+i+"]:"+svs.getSupervisons().get(i).getCall());
					LogUtil.i("Action","time["+"]:"+svs.getSupervisons().get(i).getTime_segments());
				}

				List<Supervision> s = MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().loadAll();
				for(int j = 0;j < s.size();j ++)
				{
					LogUtil.i("Action","db["+j+"] id:"+s.get(j).getId());
					LogUtil.i("Action","db["+j+"] eys:"+s.get(j).getEyes_protect());
					LogUtil.i("Action","db["+j+"] app:"+s.get(j).getApplication());
				}

				return svs;  
			}
			else
			{
				//				MyToast.getInstance().show("控制策略为空");
			}

		} catch (Exception e) {
			LogUtil.i("Action","e:"+e);
		}
		return null;
	}


	//	public List<Supervision> supervisionList() 
	//	{
	//		String result = NetTransportUtil.getContent("students/me/supervisions", null);
	//		Gson gson = new Gson();
	//
	//		try {
	//			
	//			Supervisions svs = gson.fromJson(result, Supervisions.class);
	//			if(null != svs)
	//			{
	////				MyApplication.getInstance().getSupervisonsDaoSession().getMySupervisionDao().deleteAll();
	////				MyApplication.getInstance().getSupervisonsDaoSession().getMySupervisionDao().insert(svs);
	////				MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().deleteAll();
	////				MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().loadDeep((long) 1);
	//				
	//				//String id = svs.getMySupervisionId();
	//				for(int i = 0; i < svs.getSupervison().size();i ++)
	//				{
	////					MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().insert(svs.getSupervison().get(i));
	//					//MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().queryBuilder();
	//					//MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().loadDeep((long) 1);
	////					svs.getSupervisons().get(i).__setDaoSession(MyApplication.getInstance().getSupervisonsDaoSession());
	////					List<Supervision> s = MyApplication.getInstance().getSupervisonsDaoSession().getSupervisionDao().loadAll();
	////					for(int j = 0;j < s.size();j ++)
	////					{
	////						LogUtil.i("Action","db["+j+"] id:"+s.get(j).getId());
	////						LogUtil.i("Action","db["+j+"] eys:"+s.get(j).getEyes_protect());
	////						LogUtil.i("Action","db["+j+"] app:"+s.get(j).getApplication());
	////					}
	//					
	//					
	//					LogUtil.i("Action","id:"+svs.getSupervison().get(i).getId());
	//					LogUtil.i("Action","eyes:"+svs.getSupervison().get(i).getEyes_protect());
	//					LogUtil.i("Action","app:"+svs.getSupervison().get(i).getApplication());
	//					LogUtil.i("Action","call:"+svs.getSupervison().get(i).getCall());
	//					LogUtil.i("Action","time:"+svs.getSupervison().get(i).getTime_segments());
	//				}
	//				return svs.supervisons;  
	//			}
	//			else
	//			{
	//				MyToast.getInstance().show("控制策略为空");
	//			}
	//			
	//		} catch (Exception e) {
	//			LogUtil.i("Action","e:"+e);
	//		}
	//		return null;
	//	}


	public TestMySupervision testsupervisions() 
	{
		String result = NetTransportUtil.getContent("students/me/supervisions", null);
		Gson gson = new Gson();

		try {

			TestMySupervision svs = gson.fromJson(result, TestMySupervision.class);
			if(null != svs)
			{

				return svs;
			}
			else
			{
				MyToast.getInstance().show("控制策略为空");
			}

		} catch (Exception e) {
			LogUtil.i("Action","e:"+e);
		}
		return null;
	}

	private class Sp
	{
		private List<SpecialNums> special_nums;

		public List<SpecialNums> getSpecial_nums() {
			return special_nums;
		}

		public void setSpecial_nums(List<SpecialNums> special_nums) {
			this.special_nums = special_nums;
		}
	};
	public List<SpecialNums> specialnums() 
	{

		String result = NetTransportUtil.getContent("students/me/special_nums", null);
		Gson gson = new Gson();
		try {

			Sp sp = gson.fromJson(result, Sp.class);
			if(null != sp)
			{

				return sp.getSpecial_nums();
			}
			else
			{
				MyToast.getInstance().show("特殊号码为空");
			}

		} catch (Exception e) {
			LogUtil.i("Action","e:"+e);
		}
		return null;
	}

	public User ModifyMe(User user) 
	{

		Map<String,String> userMap = new HashMap<String, String>();	
		userMap.put("name", user.getName());
		userMap.put("gender", user.getGender());
		userMap.put("birthday", user.getBirthday());
		userMap.put("avatar_url", user.getAvatar_url());
		String result = NetTransportUtil.getContentByPatch("users/me", userMap);
		Gson gson = new Gson();
		try {

			userJson userJson = gson.fromJson(result, userJson.class);
			if(null != userJson && null != userJson.getUser())
			{
				return userJson.getUser();
			}
			else
			{
				MyToast.getInstance().show( "个人信息更新失败");
			}

		} catch (Exception e) {
			LogUtil.i("Action","e:"+e);
		}
		return null;
	}

	public User modifyPhone(String phone,String smsCode,String password) 
	{

		Map<String,String> userMap = new HashMap<String, String>();	
		userMap.put("phone", phone);
		userMap.put("smscode", smsCode);
		userMap.put("password", password);
		String result = NetTransportUtil.getContentByPut("users/me/phone", userMap);
		Gson gson = new Gson();
		try {

			userJson userJson = gson.fromJson(result, userJson.class);
			if(null != userJson && null != userJson.getUser())
			{
				return userJson.getUser();
			}
			else
			{
				MyToast.getInstance().show( "更改电话号码失败");
			}

		} catch (Exception e) {
			LogUtil.i("Action","e:"+e);
		}
		return null;
	}
	
	private  class Result
	{
		private String result;
		public static final String Succ = "success";

		public String getResult() {
			return result;
		}

		public void setResult(String result) {
			this.result = result;
		}
	}
	
	public boolean modifyPassword(String old_password,String password) 
	{

		Map<String,String> userMap = new HashMap<String, String>();	
		userMap.put("old_password", old_password);
		userMap.put("password", password);
		String result = NetTransportUtil.getContentByPut("users/me/password", userMap);
		Gson gson = new Gson();
		try {

			Result resultjson = gson.fromJson(result, Result.class);
			if(null != resultjson)
			{
				if(Result.Succ.equals(resultjson.getResult()))
				{
					return true;
				}
				else
				{
					MyToast.getInstance().show( "更改密码失败:"+resultjson.getResult());
				}
			}
			else
			{
				MyToast.getInstance().show( "更改密码失败:");
			}

		} catch (Exception e) {
			LogUtil.i("Action","e:"+e);
		}
		
		return false;
	}
}
